<template>
	<view class="gudong">
		<view class="gudongxx" v-for="(item,index) in allData" :key="index" @click="goyuanglib(item.id)">
			<image class="renwtoux" v-if="item.image.length==0"
				src="https://img1.baidu.com/it/u=2806775588,1303337175&fm=253&fmt=auto&app=138&f=JPEG?w=711&h=500"
				mode="aspectFill" />
			<image class="renwtoux" v-else :src="imgUrl+item.image[0]" mode="aspectFill"></image>

			<view>
				<view class="name">{{item.mdname}}</view>
				<view class="touzqink">
					<view>员工{{item.yuangong.length}}人</view>
					<view>门店状态：{{item.state}}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import qqmapsdk from '@/sdk/qqmap-wx-jssdk.js'

	export default {
		data() {
			return {
				imgUrl: this.$imgUrl,
				latitude: '',
				longitude: '',
				province: '',
				city: '南宁',
				yg_id: '',
				mendian_id: '',
				dqjs: '',
				area: '西乡塘',
				allData: [], //所有数据
				key: 'TFXBZ-WKMK3-QHG3Q-YF7IY-SLEIZ-6CFHQ',
				getDataFlag: false
			}
		},
		onLoad() {
			let that = this;
			that.getLocation();
			uni.getStorage({
				key: 'user_key',
				complete: function(res) {
					// console.log(666, res)
					that.yg_id = res.data.id
					that.dqjs = res.data.role
					that.mendian_id = res.data.mendian
					//开始根据城市来获取门店列表
					// console.log("dqjs:"+that.dqjs)
					if (that.dqjs == 3 && that.getDataFlag == true) {
						//查询区域
						that.getAreaStoreList()
					}
					if (that.dqjs == 4 && that.getDataFlag == true) {
						//查询城市
						that.getCityStoreList();
					}
					if (that.dqjs == 5) {
						//老板角色查询所有
						that.getStoreList();
					}

				}
			})
		},
		onShow() {


		},
		methods: {
			//老板获取所有的门店列表
			getStoreList() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/GetMendian',
					method: 'GET',
					data: {
						latitude: 0,
						longitude: 0
					},
					header: {
						"Content-Type": "application/json"
					},
					success: function(res) {
						// console.log(555,res)
						if (res.data.code == 200 && res.data.msg == '查询成功') {
							_that.allData = res.data.data;

						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: "请求出现异常！"
							});
						}
					}
				})
			},
			//区域经理代表获取旗下的所有门店
			getAreaStoreList() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/GetMendian',
					method: 'GET',
					data: {
						latitude: _that.latitude,
						longitude: _that.longitude,
						area: _that.area
					},
					header: {
						"Content-Type": "application/json"
					},
					success: function(res) {
						// console.log(555,res)
						if (res.data.code == 200 && res.data.msg == '查询成功') {
							_that.allData = res.data.data;
						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: "请求出现异常！"
							});
						}
					}
				})
			},
			//市级经理代表获取旗下的所有门店
			getCityStoreList() {
				let _that = this;
				uni.request({
					url: getApp().globalData.url + '/client/GetMendian',
					method: 'GET',
					data: {
						latitude: _that.latitude,
						longitude: _that.longitude,
						city: _that.city
					},
					header: {
						"Content-Type": "application/json"
					},
					success: function(res) {
						// console.log(555, res)
						if (res.data.code == 200 && res.data.msg == '查询成功') {
							_that.allData = res.data.data;
						} else {
							uni.showToast({
								icon: 'none',
								position: 'center',
								title: "请求出现异常！"
							});
						}
					}
				})
			},
			//获取当前城市
			async getLocation() {
				const QQMapWX = new qqmapsdk({
					key: 'TFXBZ-WKMK3-QHG3Q-YF7IY-SLEIZ-6CFHQ'
				});
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: (res) => {
						console.log("获取经纬度成功");
						this.latitude = res.latitude;
						this.longitude = res.longitude;
					},
					fail: () => {
						console.log("获取经纬度失败");
					},
					complete: () => {
						// 解析地址
						QQMapWX.reverseGeocoder({
							location: {
								latitude: this.latitude,
								longitude: this.longitude
							},
							success: function(res) {
								console.log("解析地址成功");
								// 省
								let province = res.result.ad_info.province;
								this.province = province;
								// 市
								let city = res.result.ad_info.city;
								this.city = city
								// console.log(province);
								// console.log(city);
							},
							fail: function(res) {
								uni.showToast({
									title: '定位失败',
									duration: 2000,
									icon: "none"
								})
								// console.log(res);
							},
							complete: function(res) {
								// console.log(res);
								this.area = res.result.address_component.district;
								this.getDataFlag = true;
							}
						})
					}
				})

			},
			goyuanglib(id) {
				uni.navigateTo({
					url: '../yuanglist/yuanglist?mendian_id=' + id
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.gudong {
		padding: 20rpx;

		.biaoxx {
			display: flex;
			justify-content: space-between;
			align-items: center;

			.chakgd {
				color: #55AAFF;
			}
		}

		.gudongxx {
			display: flex;
			align-items: center;
			background: #f0f0f0;
			padding: 20rpx;
			margin-top: 20rpx;
			border-radius: 20rpx;

			.renwtoux {
				width: 80rpx;
				height: 80rpx;
				border-radius: 40rpx;
				margin-right: 10rpx;
			}

			.name {
				font-weight: bold;
				margin-bottom: 5rpx;
			}

			.touzqink {
				display: flex;
				align-items: center;
				width: 560rpx;
				justify-content: space-between;
				font-size: 26rpx;
				color: #3F536E;
			}
		}
	}
</style>
